---
layout: "default"
title: "CaseIterable"
description: "Swift documentation for 'CaseIterable': A type that provides a collection of all of its values."
keywords: "CaseIterable,protocol,swift,documentation,allCases,AllCases"
root: "/v4.2"
---

<div class="intro-declaration"><code class="language-swift">protocol CaseIterable</code></div>

<div class="discussion comment">
    <p>A type that provides a collection of all of its values.</p>

<p>Types that conform to the <code>CaseIterable</code> protocol are typically
enumerations without associated values. When using a <code>CaseIterable</code> type,
you can access a collection of all of the type&#39;s cases by using the type&#39;s
<code>allCases</code> property.</p>

<p>For example, the <code>CompassDirection</code> enumeration declared in this example
conforms to <code>CaseIterable</code>. You access the number of cases and the cases
themselves through <code>CompassDirection.allCases</code>.</p>

<pre><code class="language-swift">enum CompassDirection: CaseIterable {
    case north, south, east, west
}

print(&quot;There are \(CompassDirection.allCases.count) directions.&quot;)
// Prints &quot;There are 4 directions.&quot;
let caseList = CompassDirection.allCases
                               .map({ &quot;\($0)&quot; })
                               .joined(separator: &quot;, &quot;)
// caseList == &quot;north, south, east, west&quot;</code></pre>

<h1>Conforming to the CaseIterable Protocol</h1>

<p>The compiler can automatically provide an implementation of the
<code>CaseIterable</code> requirements for any enumeration without associated values
or <code>@available</code> attributes on its cases. The synthesized <code>allCases</code>
collection provides the cases in order of their declaration.</p>

<p>You can take advantage of this compiler support when defining your own
custom enumeration by declaring conformance to <code>CaseIterable</code> in the
enumeration&#39;s original declaration. The <code>CompassDirection</code> example above
demonstrates this automatic implementation.</p>
</div>

<table class="standard">

<tr>
<th id="aliases">Associated Types</th>
<td>
<span id="aliasesmark"></span>
<div class="declaration">
<code class="language-swift">AllCases : Collection</code>
<div class="comment">
    <p>A type that can represent a collection of all values of this type.</p>
</div>
</div>
</td>
</tr>


<tr>
<th>Import</th>
<td><code class="language-swift">import Swift</code></td>
</tr>

</table>



<h3>Static Variables</h3>
<div class="declaration" id="static-var-allcases_-self-allcases">
<a class="toggle-link" data-toggle="collapse" href="#comment-static-var-allcases_-self-allcases">static var allCases: Self.AllCases</a> <span class="required">Required</span><div class="comment collapse" id="comment-static-var-allcases_-self-allcases"><div class="p">
    <p>A collection of all values of this type.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">static var allCases: Self.AllCases { get }</code>

    </div></div>
</div>






